package com.epalmpay.controller.apiweixin;


import com.epalmpay.commom.BaseController;
import com.epalmpay.entity.*;
import com.epalmpay.exception.ApplicationException;
import com.epalmpay.mapper.OrderGoodMapper;
import com.epalmpay.mapper.OrderServiceGoodMapper;
import com.epalmpay.service.spot.ISpotService;
import com.epalmpay.status.Rescode;
import com.epalmpay.dto.member.MemberInDTO;
import com.epalmpay.dto.member.MemberOutDTO;
import com.epalmpay.dto.order.OrderInDTO;
import com.epalmpay.dto.system.BaseOutDTO;
import com.epalmpay.dto.system.TradeOfflineBaseOutDTO;
import com.epalmpay.enumdef.BizEnum;
import com.epalmpay.enumdef.CommonEnum;
import com.epalmpay.service.activity.IRedPackageUserService;
import com.epalmpay.service.group.IGroupService;
import com.epalmpay.service.mall.IMallCategoryService;
import com.epalmpay.service.management.ITemplateMailSendService;
import com.epalmpay.service.member.IMemberService;
import com.epalmpay.service.order.IOrderService;
import com.epalmpay.service.sms.ISMSService;
import com.epalmpay.service.trade.ITradeService;
import com.epalmpay.service.weixin.ITokenService;
import com.epalmpay.service.weixin.IWxApiMemberService;
import com.epalmpay.service.weixin.IWxApiService;
import com.epalmpay.service.weixin.IWxApiWalletService;
import com.epalmpay.service.weixin.IWxBaseService;
import com.epalmpay.util.*;
import com.epalmpay.util.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * 
* 类名称：WxApiFindShopController.java
* 类描述： 钱包首页接口
* @author wzq
* 创建时间：2017年7月4日
 */
@Controller
@RequestMapping(value="/api/member")
public class WxApiMemberController extends BaseController{
	
	/**
	 * 
	 */
	@Resource
	private IWxApiMemberService wxApiMemberService;
	@Resource
	private IGroupService groupService;
	@Resource
	private IMemberService memberService;
	@Resource
	private IRedPackageUserService redPackageUserService;
	@Resource
	private IMallCategoryService mallCategoryService;
	@Resource
	private IWxApiService wxApiService;
//	@Autowired
//	private ITokenService tokenService;
	@Value("${WX_Key}")
    private String wXKey;

	@Autowired
	private FileUpload fileUpload;

	@Resource
	private IWxApiWalletService wxApiWalletServiceService;
	@Autowired
	private ITemplateMailSendService mailSendService;
	
	@Autowired
    protected IWxBaseService wxBaseService;
	
	@Autowired
    protected ISMSService smsService;
	@Autowired
	private ITradeService tradeService;

	@Resource
	private ISpotService spotService;

	@Resource
	private OrderServiceGoodMapper orderServiceGoodMapper;
	
	@Autowired 
    @Lazy
	private ITemplateMailSendService templateMailSendService;
	
	@Resource  
    private IOrderService orderService;

	@Resource
	private OrderGoodMapper orderGoodMapper;
	
	/**根据memberId查询会员信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getmemberinfo", method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public Response getMemberInfo(String memberId) throws Exception{
		logBefore(logger, "根据memberId查询会员信息");
		 if(Tools.isEmpty(memberId)){ 
				return Response.getError("memberId不能为空");
		 }
		 String memberIds=memberId;
//		 String memberIds=AES.AESDncode(wXKey, memberId);
//		 if(memberIds==null){
//			 return Response.getError("非法参数");
//		 }
		try {
			Member me=new Member();
			me.setId(Long.parseLong(memberIds));
			Member member=memberService.findMemberinfo(me);
			if(member!=null){
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("user_id", member.getId());
					System.out.println(member.getMemberGrade().getName());
					map.put("user_level", member.getMemberGradeName()); 
					map.put("user_store", "");//会员所属门店
					if(member.getFromType()!=null){
						map.put("user_from", BizEnum.MemberDevelopeFrom.fromType(member.getFromType()).getName());
					}
					map.put("user_tele", member.getPhone());
					if(member.getWxsubStaus()!=null){
						map.put("user_follow", BizEnum.MemberStatus.fromType(member.getWxsubStaus()).getName());
					}
					DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					map.put("user_addtime", fmt.format(member.getGmtCreate())); 
					if(member.getWalletStatus()==0){
						map.put("packet_usestatus", CommonEnum.WalletStatus.close.getName());
					}else if(member.getSex().equals(CommonEnum.WalletStatus.opened.getType())){
						map.put("packet_usestatus", CommonEnum.WalletStatus.opened.getName());
					}
					
					if(member.getSex()!=null){
						map.put("user_sex", CommonEnum.SexType.fromType(member.getSex()).getName());
					}
					if(member.getBirthday()==null){
						map.put("user_birth", ""); 
					}else{
						map.put("user_birth", DateUtil.fomatDate(member.getBirthday(),DateUtil.sdfDay));
					}
					map.put("user_addr", member.getAddress());
					map.put("card_no", member.getCardNo());
					map.put("weixin_name", member.getWeixinName());
					
					if(member.getApproveStatus()!=null&&member.getApproveStatus()==1){
						map.put("approve_status", "1"); 
						map.put("user_realname", member.getName());
						map.put("user_idc", member.getIdNo());
					}else{
						map.put("approve_status", "0"); 
						map.put("user_realname", "");
						map.put("user_idc", "");
					}
					return Response.getSuccess(map);
			}else{
				return Response.getError("未查询到记录");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return Response.getError("服务器异常");
		}
		
	}
	
	/**
	 * 获取微信信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getweixininfo", method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public Response getweixininfo(@RequestParam(required = true) String memberId) throws Exception {
		logBefore(logger, "微信信息");
		try {
			MemberInDTO memberInDTO = new MemberInDTO();
			memberInDTO.setId(Long.parseLong(memberId));
			MemberOutDTO member = memberService.queryAmount(memberInDTO);
			Map<String, Object> map = wxApiService.queryWxInfo(memberId);
			if(member != null) {
				map.put("allmoney", (Integer.parseInt(member.getCurBal()) + Integer.parseInt(member.getCurBalB()) + Integer.parseInt(member.getLockBal()) + Integer.parseInt(member.getLockBalB())));
				map.put("lockmoney", (Integer.parseInt(member.getLockBal()) + Integer.parseInt(member.getLockBalB())));
				map.put("canmoney", Integer.parseInt(member.getCurBal()));
				map.put("canbmoney", Integer.parseInt(member.getCurBalB()));
			} else {
				map.put("allmoney", 0);
				map.put("lockmoney", 0);
				map.put("canmoney", 0);
				map.put("canbmoney", 0);
			}
			if(map == null || map.size()==0){
				throw new ApplicationException(Rescode.FAIL,"未查询到记录");
			}
			return Response.getSuccess(map);
		} catch (ApplicationException ae) {
			Map<String, Object> map = wxApiService.queryWxInfo(memberId);
			if(map == null || map.size()==0){
				throw new ApplicationException(Rescode.FAIL,"未查询到记录");
			}
			return Response.getSuccess(map);
			/*logger.error(ae);
			return Response.getError(ae.errMsg);*/
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
	}
	
	
	/**根据groupId查询栏目
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/findCategory", method = {RequestMethod.GET,RequestMethod.POST} )
	@ResponseBody
	public Response findCategory2(@RequestParam(required = true) String groupId,
								  @RequestParam(required = true) String level,
								  @RequestParam(required = false) Integer goodBigtype) throws Exception{
		logBefore(logger, "根据groupId查询会员信息");
		Map<String, Object> map = null;

		try {
			map = wxApiService.findCategory(groupId,level,goodBigtype);
			if(map == null || map.size()==0){
				throw new ApplicationException(Rescode.FAIL,"未查询到记录");
			}
			return Response.getSuccess(map);
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
	}
	
	
	/**
	 * 查询会员签到列表
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/membersignlist", method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public Response memberSignList(@RequestParam(required = true) String groupId,
							 @RequestParam(required = true) String memberId) throws Exception {
		logBefore(logger, "签到保存");
		Map<String, Object> map = null;

		try {
			map = wxApiService.memberSignList(groupId,memberId);
			if(map == null || map.size()==0){
				throw new ApplicationException(Rescode.FAIL,"未查询到记录");
			}
			return Response.getSuccess(map);
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
	}
	
	
	//已领优惠券
	@RequestMapping(value = "/couponhavegottenlist", method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public Response couponHavegottenList(  @RequestParam(required =true) String memberId,
										  @RequestParam(required =false) Integer amount ){
		Map<String, Object> map = null;

		try {
			map = wxApiService.queryCouponHaveGottenList(memberId,amount);
			if(map == null || map.size()==0){
				throw new ApplicationException(Rescode.FAIL,"未查询到记录");
			}
			return Response.getSuccess(map);
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
		
	}

	/**
	 * 已领优惠券详情
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/couponhavegottendetail", method = RequestMethod.POST)
	@ResponseBody
	public Response couponhavegottendetail(@RequestParam(required = true) String memberId,
									   @RequestParam(required = true) String couponuserId) throws Exception {
		logBefore(logger, "领取优惠卷详情");
		Map<String, Object> map = null;
		try {
			map = wxApiMemberService.couponhavegottendetail(memberId,couponuserId);
			if(map == null || map.size()==0){
				throw new ApplicationException(Rescode.FAIL,"未查询到记录");
			}
			return Response.getSuccess(map);
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
	}


		/**
		 * 已使用优惠卷列表
		 * @param openId
		 * @param memberId
		 * @return
		 */
		@RequestMapping(value = "/couponhaveusedlist", method = {RequestMethod.GET,RequestMethod.POST})
		@ResponseBody
		public Response couponhaveusedlist(@RequestParam(required =true) String openId,
											 @RequestParam(required =true) String memberId){
			Map<String, Object> map = null;

			try {
				map = wxApiService.queryCouponhaveusedlist(memberId);
				if(map == null || map.size()==0){
					throw new ApplicationException(Rescode.FAIL,"未查询到记录");
				}
				return Response.getSuccess(map);
			} catch (ApplicationException ae) {
				logger.error(ae);
				return Response.getError(ae.errMsg);
			}
			catch (Exception e) {
				logger.error(e);
				return Response.getError("服务器异常");
			}
			
		}
		
		
		/**
		 * 门店商品订单列表
		 * @param openId
		 * @param memberId
		 * @param orderStatus
		 * @param pageNo
		 * @param pageSize
		 * @return
		 */
		@RequestMapping(value = "/shopgoodorderlist", method = {RequestMethod.GET,RequestMethod.POST})
		@ResponseBody
		public Response shopGoodOrderList(@RequestParam(required =true) String openId,
											 @RequestParam(required =true) String memberId,
											 @RequestParam(required =true) String orderStatus,
											 @RequestParam(required =true) Integer pageNo,
											 @RequestParam(required =true) Integer pageSize){
			Map<String, Object> map = null;

			try {
				map = wxApiService.queryShopGoodOrderList(openId,memberId,orderStatus,pageNo,pageSize);
				if(map == null || map.size()==0){
					throw new ApplicationException(Rescode.FAIL,"未查询到记录");
				}
				return Response.getSuccess(map);
			} catch (ApplicationException ae) {
				logger.error(ae);
				return Response.getError(ae.errMsg);
			}
			catch (Exception e) {
				logger.error(e);
				return Response.getError("服务器异常");
			}
			
		}
		
		/**
		 * 取消订单
		 *
		 * @return
		 * @throws Exception
		 */
		@RequestMapping(value = "/ordercancel", method = {RequestMethod.GET,RequestMethod.POST})
		@ResponseBody
		public Response orderCancel(@RequestParam(required = true) String memberId,
									  @RequestParam(required = true) String orderId) throws Exception {
			logBefore(logger, "取消订单");

			try {
				wxApiService.updateOrderStatus(memberId,orderId,ROOT_HOME_URL);
				return Response.getSuccess(null);
			} catch (ApplicationException ae) {
				logger.error(ae);
				return Response.getError(ae.errMsg);
			}
			catch (Exception e) {
				logger.error(e);
				return Response.getError("服务器异常");
			}
		}
		
		
		@RequestMapping(value = "/serviceGoodRefund", method = {RequestMethod.GET,RequestMethod.POST})
		@ResponseBody
		public Response serviceGoodRefund(String memberId,String orderId,String refundReason) throws Exception {
			logBefore(logger, "服务商品订单退款");
			HashMap<String, String> resultMap = new HashMap<String, String>();
			resultMap.put("会员编号", memberId);
			resultMap.put("订单编号", orderId);
			String nullParam=Response.checkNull(resultMap);
			if (null != nullParam) {
				return Response.getError(nullParam+"不能为空");
			}
			
			BaseOutDTO baseOutDTO = new BaseOutDTO();
			try {
				OrderInDTO orderInDTO=new OrderInDTO();
				orderInDTO.setOrderNo(orderId); 
				orderInDTO.setMemberId(Long.valueOf(memberId));
				orderInDTO.setRefundReason(refundReason); 
				TradeOfflineBaseOutDTO tradeOfflineOutDTO=orderService.editConfirmRefundServiceGoodOrder(orderInDTO);
					if(tradeOfflineOutDTO.getErrorCode().equals(Rescode.SUCCESS)){
						//微信发送模板消息	
		    				try {
								templateMailSendService.sendTemplate(tradeOfflineOutDTO.getTradeOffline(),  BizEnum.WebTemplateType.template5.getType(),
										tradeOfflineOutDTO.getTradeOffline().getPayerId(),
										tradeOfflineOutDTO.getTradeOffline().getGroupId(), BizEnum.PaymentType.WebChat.getType(),ROOT_HOME_URL);
							} catch (Exception e) {
								logger.info ("模板消息发送失败"+e); 
							}

						 try {
							 if(orderInDTO.getOrderNo().startsWith(Const.CONSUMPTION_VIRTUAL)){
//								 OrderServiceGood orderServiceGood = orderServiceGoodMapper.queryShopIdByOrderId(orderId);
								 baseOutDTO=orderService.updateRebatewithdraw(tradeOfflineOutDTO.getTradeOffline(), null,null);
							 }else{
								 OrderGood orderGood = orderGoodMapper.selectByOrderNo(orderId);
								 baseOutDTO=orderService.updateRebatewithdraw(tradeOfflineOutDTO.getTradeOffline(), null,orderGood.getShopId());
							 }



							 baseOutDTO.setErrorCode(Rescode.SUCCESS, "退款成功");
							 return Response.getSuccess(Response.SUCCESS_CODE, "退款成功");
						} catch (ApplicationException ae) { 
							baseOutDTO.setErrorCode(ae.errCode, ae.errMsg);
							baseOutDTO.setErrorCode(Rescode.SUCCESS, "退款成功"+ae.errMsg);
							ae.printStackTrace();
							logger.error(ae);
						}catch (Exception e) {
							e.printStackTrace();
							logger.error(e);
							return Response.getSuccess(Response.SUCCESS_CODE, "退款成功,优惠福利未退回");
						  }
					  }
				return Response.getError(tradeOfflineOutDTO.getErrorMsg());
			} catch (ApplicationException ae) { 
				baseOutDTO.setErrorCode(ae.errCode, ae.errMsg);
				ae.printStackTrace();
				logger.error(ae);
			}
			 catch (Exception e) { 
				e.printStackTrace();
				logger.error(e);
				return Response.getError("处理出现异常");
			}
			return Response.getError("处理失败");
		}
		
		

		/**
		 * 订单详情
		 *
		 * @return
		 * @throws Exception
		 */
		@RequestMapping(value = "/orderdetail", method = {RequestMethod.GET,RequestMethod.POST})
		@ResponseBody
		public Response orderDetail( String memberId, String orderId) throws Exception {
			logBefore(logger, "订单详情");
			HashMap<String, String> resultMap = new HashMap<String, String>();
			resultMap.put("会员编号", memberId);
			resultMap.put("订单编号", orderId);
			String nullParam=Response.checkNull(resultMap);
			if (null != nullParam) {
				return Response.getError(nullParam+"不能为空");
			}
			
			Map<String, Object> map = null;
			try {
				map = wxApiService.queryOrderDetail(memberId,orderId);
				if(map == null || map.size()==0){
					throw new ApplicationException(Rescode.FAIL,"未查询到记录");
				}
				return Response.getSuccess(map);
			} catch (ApplicationException ae) {
				logger.error(ae);
				return Response.getError(ae.errMsg);
			}
			catch (Exception e) {
				logger.error(e);
				return Response.getError("服务器异常");
			}
		}
		
		/**
		 * 订单退款
		 * @param memberId
		 * @param orderId
		 * @return
		 * @throws Exception
		 */
		@RequestMapping(value = "/orderrefund", method = {RequestMethod.GET,RequestMethod.POST})
		@ResponseBody
		public Response orderRefund(@RequestParam(required = true) String memberId,
								 @RequestParam(required = true) String orderId) throws Exception {
			logBefore(logger, "订单详情");
			Map<String, Object> map = null;
			try {
				map = wxApiService.orderRefund(memberId,orderId);
				if(map == null || map.size()==0){
					throw new ApplicationException(Rescode.FAIL,"未查询到记录");
				}
				return Response.getSuccess(map);
			} catch (ApplicationException ae) {
				logger.error(ae);
				return Response.getError(ae.errMsg);
			}
			catch (Exception e) {
				logger.error(e);
				return Response.getError("服务器异常");
			}
		}
		
		
		/**
		 * 领取红包
		 * @param memberId
		 * @param redpackageUserId
		 * @return
		 * @throws Exception
		 */ 
		@RequestMapping(value = "/redpackageget", method = {RequestMethod.GET,RequestMethod.POST})
		@ResponseBody
		public Response redpackageGet(String memberId,String redpackageUserId) throws Exception {
			logBefore(logger, "领取红包");
	       HashMap<String, String> mapCheck = new HashMap<String, String>();
	       mapCheck.put("订单编号", memberId);
	       mapCheck.put("支付金额", redpackageUserId+"");
		   String nullParam=Response.checkNull(mapCheck);  
			if (null != nullParam) {
				return Response.getError(nullParam+"不能为空");
			}
			Map<String, Object> map = null;
			try {
				map = wxApiMemberService.insertRedpackageUser(memberId,redpackageUserId);
				if(map == null || map.size()==0){
					throw new ApplicationException(Rescode.FAIL,"未查询到记录");
				}
				return Response.getSuccess(map);
			} catch (ApplicationException ae) {
				logger.error(ae);
				return Response.getError(ae.errMsg);
			}
			catch (Exception e) {
				logger.error(e);
				return Response.getError("服务器异常");
			}
		}



		/**
		 * 用电话号码查姓名
		 * @param groupId
		 * @param phone
		 * @return
		 * @throws Exception
		 */
		@RequestMapping(value = "/getnamebyphone",  method = {RequestMethod.GET,RequestMethod.POST})
		@ResponseBody
		public Response getNameByPhone( String groupId, String phone) throws Exception {
			Map<String, Object> map = null;
			HashMap<String, String> resultMap = new HashMap<String, String>();
			resultMap.put("商户编号", groupId);
			resultMap.put("手机号码", phone);
			String nullParam=Response.checkNull(resultMap);
			if (null != nullParam) {
				return Response.getError(nullParam+"不能为空");
			}
			try {
				map = wxApiService.getNameByPhone(phone,groupId);
				if(map == null || map.size()==0){
					throw new ApplicationException(Rescode.FAIL,"未查询到记录");
				}
				return Response.getSuccess(map);
			} catch (ApplicationException ae) {
				logger.error(ae);
				return Response.getError(ae.errMsg);
			}
			catch (Exception e) {
				logger.error(e);
				return Response.getError("服务器异常");
			}	
			
		}


		/**
		 * 密码修改
		 *
		 * @return
		 * @throws Exception
		 */
		@RequestMapping(value = "/updatepassword", method = {RequestMethod.GET,RequestMethod.POST})
		@ResponseBody
		public Response updatePassword( String memberId, String oldPassword, String newPassword,
									  String type) throws Exception {
			logBefore(logger, "密码修改");
			HashMap<String, String> resultMap = new HashMap<String, String>();
			resultMap.put("会员编号", memberId);
			resultMap.put("原密码", oldPassword);
			resultMap.put("新密码", newPassword);
			resultMap.put("密码类型", type);
			String nullParam=Response.checkNull(resultMap);
			if (null != nullParam) {
				return Response.getError(nullParam+"不能为空");
			}
			Map<String, Object> map = new HashMap<String, Object>();
			try {
				if (type.equals("1")) {//修改登录 密码 
					wxApiService.updatePassword(memberId,newPassword,oldPassword);
					mailSendService.sendMemberTemplate(Long.valueOf(memberId), BizEnum.WebTemplateType.template14.getType(), ROOT_HOME_URL,"登录");
				} else if (type.equals("3")){ //第一次修改支付密码，不需要原始密码
					wxApiService.updateTradePasswordOne(memberId, newPassword);
					mailSendService.sendMemberTemplate(Long.valueOf(memberId), BizEnum.WebTemplateType.template14.getType(), ROOT_HOME_URL,"支付");
				} else{
					wxApiService.updateTradePassword(memberId,newPassword,oldPassword);
					mailSendService.sendMemberTemplate(Long.valueOf(memberId), BizEnum.WebTemplateType.template14.getType(), ROOT_HOME_URL,"支付");
				}
				return Response.getSuccess(map);
			} catch (ApplicationException ae) {
				logger.error(ae);
				return Response.getError(ae.getErrMsg());
			}
			catch (Exception e) {
				logger.error(e);
				return Response.getError("服务器异常");
			}
		}
		
		
		/**
		 * 密码重置
		 *
		 * @return
		 * @throws Exception
		 */
		@RequestMapping(value = "/resetpassword", method = {RequestMethod.GET,RequestMethod.POST})
		@ResponseBody
		public Response resetpassword( String memberId, String phone, String password, String type, String code) throws Exception {
			logBefore(logger, "密码修改");
			Map<String, Object> map = new HashMap<String, Object>();
			try {
				HashMap<String, String> resultMap = new HashMap<String, String>();
				resultMap.put("会员编号", memberId);
				resultMap.put("新密码", password);
				resultMap.put("手机号码", phone);
				resultMap.put("短信验证码", code);
				resultMap.put("短信类型", type);
				String nullParam=Response.checkNull(resultMap);
				if (null != nullParam) {
					return Response.getError(nullParam+"不能为空");
				}
				//获取验证码
				RedisUtil redisUtil = new RedisUtil();
				Jedis jedis = redisUtil.getJedis();
				String codemessage = jedis.get(Const.SESSION_PHONE_CODE+phone+type);
				//String codemessage=(String)Const.CacheMapObject.get(Const.SESSION_PHONE_CODE+phone+type);
				logBefore(logger, "原验证码"+codemessage);
				if(Tools.isEmpty(codemessage)||!codemessage.equals(code)){ 
					return Response.getError("验证码输入错误");
				}
				if (Integer.parseInt(type)==2) {//tyep=1 修改登入密码 tyep =2 表示支付密码                  1表示钱包注册 2表示重置登录密码 3表示重置支付密码
					wxApiService.passwordUpdate(memberId,password);
				}else if (Integer.parseInt(type)==3){
					wxApiService.updatetradePassword(memberId,password);
				}
				jedis.del(Const.SESSION_PHONE_CODE+phone+type);
				//Const.CacheMapObject.remove(Const.SESSION_PHONE_CODE+phone+type);
				map.put("message", "处理成功");
				jedis.quit();
				return Response.getSuccess(map);
			} catch (ApplicationException ae) {
				logger.error(ae);
				return Response.getError(ae.getErrMsg());
			}
			catch (Exception e) {
				logger.error(e);
				return Response.getError("服务器异常");
			}
		}

	/**
	 * 发送短信验证码
	 * @param type
	 * @param phone
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getverifycode", method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public Response sendCode(String phone,String type,Long groupId) throws Exception {
		logBefore(logger, "发送短信验证码");
		HashMap<String, String> resultMap = new HashMap<String, String>();
		resultMap.put("手机号码", phone);
		resultMap.put("短信类型", type);
		String nullParam=Response.checkNull(resultMap);
		if (null != nullParam) {
			return Response.getError(nullParam+"不能为空");
		}
		Map<String,String> map = new HashMap<String,String>();
		Date date=new Date();
		RedisUtil redisUtil = new RedisUtil();
		Jedis jedis = redisUtil.getJedis();
		//短信验证码2分钟发送一次 验证码有效时间30分钟
		//Date verifyDate=(Date)Const.CacheMapObject.get(Const.SESSION_TiME_CODE+phone+type);
		Long verifyDate = jedis.ttl(Const.SESSION_PHONE_CODE+phone+type);
		if(verifyDate!=null){
			if(verifyDate >= 1680){//如果剩余生成时间大于1680秒，则抛出异常
				return Response.getError("2分钟内只能发送1条校验码。");
			}
		}
		try {
			String code="true".equals("false") ? "000000" : String.format("%.0f",(Math.random()*9+1)*100000);
			this.logger.info("---验证码---:"+code);
			Map<String, String> params=new HashMap<String, String>();
			params.put("type", type);
			boolean flag = false;
			GroupPayconfig groupPayconfig = smsService.selectConfigByGroupid(groupId);
			GroupWxtemplate groupWxtemplate = smsService.selectTemplateConfigByGroupid(groupId,Integer.parseInt(type));
			if(groupPayconfig != null) {
				flag=smsService.sendRegionCode(groupId, code, groupWxtemplate.getTemplateType(), phone, params);
			} else {
				flag = smsService.sendRegionCode(groupId, code, BizEnum.SMSTemplateType.temp_SMS_104630027.getType(), phone, params);
			}
			if(flag){
				//设置值
				jedis.setex(Const.SESSION_PHONE_CODE+phone+type, 1800, code);
				//Const.CacheMapObject.put(Const.SESSION_PHONE_CODE+phone+type,code);
				//Const.CacheMapObject.put(Const.SESSION_TiME_CODE+phone+type, date);
			}
			jedis.quit();
			return Response.getSuccess(map);
		} catch (Exception e) {
			e.printStackTrace();
			return Response.getError("验证码发送异常");
		}

	}
		
		
		/**
		 * 会员钱包权限
		 * @param memberId
		 * @return
		 * @throws Exception
		 */
		@RequestMapping(value = "/walletCompetence", method = {RequestMethod.GET,RequestMethod.POST})
		@ResponseBody
		public Response walletCompetence(String memberId) throws Exception {
			logBefore(logger, "会员钱包权限");
			HashMap<String, String> resultMap = new HashMap<String, String>();
			resultMap.put("会员编号", memberId);
			String nullParam=Response.checkNull(resultMap);
			if (null != nullParam) {
				return Response.getError(nullParam+"不能为空");
			}
			try {
				Response response=wxApiMemberService.memberWalletCompetence(Long.valueOf(memberId));
				return response;
			} catch (Exception e) {
				e.printStackTrace();
				return Response.getError("会员钱包权限获取异常");
			}
		}
		
		
		/**
		 *我的邀请福利列表
		 * @param openId
		 * @param memberId
		 * @param date
		 * @return
		 */
		@RequestMapping(value = "/myinvitewelfarelist", method = {RequestMethod.GET,RequestMethod.POST})
		@ResponseBody
		public Response myInvitewelfareList( @RequestParam(required =true) String openId,
											 @RequestParam(required =true) String memberId,
											 @RequestParam(required =true) String date,
											 @RequestParam(required =true) Integer pageNo,
											 @RequestParam(required =true) Integer pageSize){
			Map<String, Object> map = null;
			try {
				map = wxApiService.myInvitewelfareList(memberId,date,pageNo,pageSize);
				if(map == null || map.size()==0){
					throw new ApplicationException(Rescode.FAIL,"未查询到记录");
				}
				return Response.getSuccess(map);
			} catch (ApplicationException ae) {
				logger.error(ae);
				return Response.getError(ae.getErrMsg());
			}
			catch (Exception e) {
				logger.error(e);
				return Response.getError("服务器异常");
			}
			
		}
		/**
		 * 分享邀请查询
		 * @param memberId
		 * @return
		 */
		@RequestMapping(value = "/shareinvitequery", method = {RequestMethod.GET,RequestMethod.POST})
		@ResponseBody
		public Response shareInviteQuery(@RequestParam(required =true) String memberId){
			 Map<String, Object> map = null;
			
			 if(Tools.isEmpty(memberId)){ 
					return Response.getError("memberId不能为空");
			 }
			try {
				map = wxApiService.shareInviteQuery(memberId);
				if(map == null || map.size()==0){
					throw new ApplicationException(Rescode.FAIL,"未查询到记录");
				}
				return Response.getSuccess(map);
			} catch (ApplicationException ae) {
				logger.error(ae);
				return Response.getError(ae.errMsg);
			}
			catch (Exception e) {
				logger.error(e);
				return Response.getError("服务器异常");
			}
			
		}
	
	//会员签到保存
	@RequestMapping(value = "/insertmembersign", method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public Response insertmembersign(@RequestParam(required = true) Long memberId) throws Exception {
		logBefore(logger, "签到保存");

		try {
			Map<String, Object> map = wxApiService.insertMemberSign(memberId);
			if(map == null || map.size()==0){
				//throw new ApplicationException(Rescode.FAIL,"未查询到记录");
				return Response.getSuccess(null);
			}
			return Response.getSuccess(map);
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("目前没有签到活动");
		}
	}


	
	/**根据memberId查询会员余额
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/findBalance", method = {RequestMethod.GET,RequestMethod.POST} )
	@ResponseBody
	public Response findBalance(String memberId) throws Exception{
		
		logBefore(logger, "根据memberId查询会员余额");
		 if(Tools.isEmpty(memberId)){ 
				return Response.getError("memberId不能为空");
		 }
		 Map<String, Object> map = new HashMap<String, Object>();
		try {
			 MemberInDTO memberInDTO=new MemberInDTO();
			 memberInDTO.setId(Long.parseLong(memberId));
			 MemberOutDTO member=memberService.queryAmount(memberInDTO);
			 if(member != null &&Tools.notEmpty(member.getVirtualCard())){
				 map.put("curBal", member.getCurBal());
				 map.put("curBalB", member.getCurBalB());
				 return Response.getSuccess(map);
			 }else{
				 return Response.getError("暂未开通钱包");
			 }
		} catch (ApplicationException e) {
			return Response.getError(e.errMsg);
		}catch (Exception e) {
			e.printStackTrace();
			return Response.getError("查询异常");
		}
	}
	
	
	/**根据订单号查询记录
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/findOrderNum", method = {RequestMethod.GET,RequestMethod.POST} )
	@ResponseBody
	public Response findOrderNum(String orderNum) throws Exception{
		logBefore(logger, "根据订单号查询记录");
		 if(Tools.isEmpty(orderNum)){ 
				return Response.getError("订单号不能为空");
		 }
		try {
			Response response=wxApiMemberService.findOrderNum(orderNum);
			return response;
		} catch (ApplicationException e) {
			return Response.getError(e.errMsg);
		}catch (Exception e) {
			e.printStackTrace();
			return Response.getError("查询异常");
		}
	}
	 
 
	/**
	 * 确认转账
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/usertransfer", method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public Response usertransfer( String memberId,
								 String toPhone,
								 Integer amount,String password,String toMemberId,Integer fee) throws Exception {
		logBefore(logger, "确认转账");
		HashMap<String, String> resultMap = new HashMap<String, String>();
		resultMap.put("会员编号", memberId);
		resultMap.put("转账金额", amount+"");
		resultMap.put("支付密码", password);
		resultMap.put("手续费", fee+"");
		String nullParam=Response.checkNull(resultMap);
		if (null != nullParam) {
			return Response.getError(nullParam+"不能为空");
		}
		if(amount<=0){
			return Response.getError("转账金额必须大于0元");
		}
		if(Tools.isEmpty(toPhone)&&Tools.isEmpty(toMemberId)){
			return Response.getError("手机号或会员编号不能同时为空");
		}
		logBefore(logger, "用户手续费--------------------------------------------------------"+fee);
		try {
		    Map<String, Object> map=wxApiMemberService.insertUserTransfer(memberId,toPhone,toMemberId,amount,password,fee,ROOT_HOME_URL);
			return Response.getSuccess(map);
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
	}

	
	/**
	 * 手续费计算
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/userFee", method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public Response userFee(String memberId,Integer tranAmt,String type) throws ApplicationException {
		logBefore(logger, "手续费计算");
		HashMap<String, String> resultMap = new HashMap<String, String>();
		resultMap.put("会员编号", memberId);
		resultMap.put("交易金额", tranAmt+"");
		resultMap.put("交易类型", type);
		String nullParam=Response.checkNull(resultMap);
		if (null != nullParam) {
			return Response.getError(nullParam+"不能为空");
		}
		try {
			Map<String, Object> map=wxApiMemberService.selectFee(memberId, tranAmt,type);
			if(map == null || map.size()==0){
				throw new ApplicationException(Rescode.FAIL,"未查询到记录");
			}
			return Response.getSuccess(map);
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
		
	}
	
	/**
	 * 支付平台手续费说明
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/userFeeExplain", method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public Response userFeeExplain(String memberId,String type) throws ApplicationException {
		logBefore(logger, "手续费说明");
		HashMap<String, String> resultMap = new HashMap<String, String>();
		resultMap.put("会员编号", memberId);
		resultMap.put("交易类型", type);
		String nullParam=Response.checkNull(resultMap);
		if (null != nullParam) {
			return Response.getError(nullParam+"不能为空");
		}
		try {
			Response feeExplain=wxApiMemberService.userFeeExplain(memberId, type);
			if(feeExplain == null){
				throw new ApplicationException(Rescode.FAIL,"未查询到记录");
			}
			return Response.getSuccess(feeExplain);
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
		
	}
	
	
	/**
	 * 2.0手续费说明
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/feeExplain", method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public Response feeExplain(String memberId,String type) throws ApplicationException {
		logBefore(logger, "手续费说明");
		HashMap<String, String> resultMap = new HashMap<String, String>();
		resultMap.put("会员编号", memberId);
		resultMap.put("交易类型", type);
		String nullParam=Response.checkNull(resultMap);
		if (null != nullParam) {
			return Response.getError(nullParam+"不能为空");
		}
		try {
			Response feeExplain=wxApiMemberService.feeExplain(memberId, type);
			return feeExplain;
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
		
	}
	
	

	/**
	 * 账单列表
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/tradedetaillist", method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public Response tradeDetailList(@RequestParam(required = true) String memberId,
									@RequestParam(required = true) Integer offOrOnLine,
									@RequestParam(required = true) Integer pageNo,
								  @RequestParam(required = true) Integer pageSize) throws Exception {
		logBefore(logger, "账单列表");
		Map<String, Object> map = null;

		try {
			map = wxApiMemberService.getTradeDetailList(memberId,offOrOnLine,pageNo,pageSize);
			if(map == null || map.size()==0){
				throw new ApplicationException(Rescode.FAIL,"未查询到记录");
			}
			return Response.getSuccess(map);
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
	}
	
	
	
	/**根据groupId查询栏目
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getmembercrcode", method = {RequestMethod.GET,RequestMethod.POST} )
	@ResponseBody
	public Response getmembercrcode(String memberId) throws Exception{
		logBefore(logger, "根据memberId生成专属二维码"+memberId);
		HashMap<String, String> resultMap = new HashMap<String, String>();
		resultMap.put("会员编号", memberId);
		String nullParam=Response.checkNull(resultMap);
		if (null != nullParam) {
			return Response.getError(nullParam+"不能为空");
		}
		try {
			Map<String, Object>  code = wxApiService.updateMembercrcode(memberId);
			if(code == null){
				return Response.getError("二维码生成失败");
			}else{
				return Response.getSuccess(code);
			}
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
	}

	/**
	 * 订单评价
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/servicegoodordercomment", method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public Response servicegoodordercomment(@RequestParam(required = true) String memberId,
											 @RequestParam(required = true) String orderId,
											@RequestParam(required = true) Integer isDisplay,
											 @RequestParam(required = true) Integer commentLevel,
											 @RequestParam(required = false) String commentContent,
											 @RequestParam(required = false) String img1,
											@RequestParam(required = false) String img2,
											@RequestParam(required = false) String img3) throws Exception {
		logBefore(logger, "服务商品订单评价");
		String path = "/ebos/weixinservicegood/comment/";

		String picUrls = "";

		if(img1 != null && StringUtil.notEmpty(img1)) {
			String result = fileUpload.upload(img1.substring(img1.indexOf(",")+1),"img1.png", path);
			if(!result.equals("failed")) {
				picUrls += result + ",";
			}
		}
		if(img2 != null && StringUtil.notEmpty(img2)) {
			String result = fileUpload.upload(img2.substring(img2.indexOf(",")+1),"img2.png", path);
			if(!result.equals("failed")) {
				picUrls += result + ",";
			}
		}
		if(img3 != null && StringUtil.notEmpty(img3)) {
			String result = fileUpload.upload(img3.substring(img3.indexOf(",")+1),"img3.png", path);
			if(!result.equals("failed")) {
				picUrls += result + ",";
			}
		}

		if(!picUrls.equals("")) {
			picUrls = picUrls.substring(0, picUrls.length() - 1);
		}

		Map<String, Object> map = null;
		try {
			map = wxApiMemberService.insertServiceGoodOrderComment(memberId,orderId,isDisplay,commentLevel,EmojiUtil.commentfilterEmoji(commentContent,""),picUrls);
			return Response.getSuccess(map);
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
	}

    /**
	 * 邀请会员参与活动
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/invitememberactivity", method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public Response inviteMemberActivity(@RequestParam(required = true) String groupId,
										 @RequestParam(required = true) String memberId,
								 @RequestParam(required = true) String newMemberId) throws Exception {
		logBefore(logger, "邀请会员参与活动");
		Map<String, Object> map = null;
		try {
			map = wxApiMemberService.insertInviteMember(Long.parseLong(groupId),
					Long.parseLong(memberId),Long.parseLong(newMemberId));
			if(map == null || map.size()==0){
				throw new ApplicationException(Rescode.FAIL,"未查询到记录");
			}
			return Response.getSuccess(map);
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
	}


	/**
	 * 添加商品到购物车 addCart
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/addCart", method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public Response addCart(@RequestParam String memberId, @RequestParam String goodsId,
								 @RequestParam Integer number, Long specId, Long sharememberid) throws Exception {
		logBefore(logger, "添加商品到购物车");
		Map<String, Object> map = null;
		try {
			 wxApiMemberService.insertMallCart(memberId,goodsId,number, specId, sharememberid);
			return Response.getSuccess(map);
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
	}

	/**
	 * 移除购物车中商品 removeCart
	 *这里的  goodsId表示购物车Id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/removeCart", method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public Response removeCart(@RequestParam String memberId, @RequestParam String goodsId) throws Exception {
		logBefore(logger, "移除购物车中商品");
		Map<String, Object> map = null;
		try {
			wxApiMemberService.deleteMallCart(memberId,goodsId);
			return Response.getSuccess(map);
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
	}

	/**
	 * 获取购物车信息
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getCart")
	@ResponseBody
	public Response getCart(@RequestParam("memberId") String memberId) {
		if(Tools.isEmpty(memberId)) {
			return Response.getError("memberId不能为空");
		}
		List<Map<String, Object>> cartList = wxApiMemberService.getMallCart(Long.parseLong(memberId));
		Map<String, Object> response = new HashMap<>();
		response.put("cartList", cartList);
		return Response.getSuccess(response);
	}

	/**
	 * 保存我的地址信息
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/saveMyAddress", method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public Response saveMyAddress(@RequestParam(required = true) String memberId,
							      @RequestParam(required = true) Integer defaultSelect,
								  @RequestParam(required = true) String addressMobilePhone,
								  @RequestParam(required = true) String addressCustomerName,
								  @RequestParam(required = true) String addressCity,
								  @RequestParam(required = true) String addressArea,
							      Long addressId, String urlAddress) throws Exception {
		logBefore(logger, "保存我的地址信息");
		Map<String, Object> map = new HashMap<>();
		map.put("urlAddress", urlAddress);
		try {
			wxApiMemberService.saveMyAddress(memberId,defaultSelect,addressMobilePhone,addressCustomerName,addressCity,addressArea,addressId);
			return Response.getSuccess(map);
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
	}

	/**
	 * 获取地址列表
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getMyAddressList", method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public Response getMyAddressList(@RequestParam(required = true) String memberId) throws Exception {
		logBefore(logger, "获取地址列表");
		Map<String, Object> map = null;
		try {
			map = wxApiMemberService.getMyAddressList(memberId);
			if(map == null){
				throw new ApplicationException(Rescode.FAIL,"未查询到记录");
			}
			return Response.getSuccess(map);
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
	}

	/**
	 * 获取缺省地址
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getDefaultAddress", method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public Response getDefaultAddress(@RequestParam(required = true) String memberId) throws Exception {
		logBefore(logger, "获取缺省地址");
		Map<String, Object> map = null;
		try {
			map = wxApiMemberService.getDefaultAddress(memberId);
			if(map == null || map.size()==0){
				throw new ApplicationException(Rescode.FAIL,"未查询到记录");
			}
			return Response.getSuccess(map);
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
	}

	/**
	 *设置默认地址
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/setDefaultAddress", method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public Response setDefaultMyAddress(@RequestParam(required = true) String memberId,
									@RequestParam(required = true) Long addressId) throws Exception {
		logBefore(logger, "删除地址信息");
		Map<String, Object> map = null;
		try {
			wxApiMemberService.setDefaultMyAddress(memberId,addressId);
			return Response.getSuccess(map);
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
	}

	/**
	 * 删除地址信息
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/deleteMyAddress", method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public Response deleteMyAddress(@RequestParam(required = true) String memberId,
							@RequestParam(required = true) Long addressId) throws Exception {
		logBefore(logger, "删除地址信息");
		Map<String, Object> map = null;
		try {
			wxApiMemberService.deleteMyAddress(memberId,addressId);
			return Response.getSuccess(map);
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
	}

	/**
	 * 获取广告列表
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getAdvertList", method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public Response getAdvertList(@RequestParam(required = true) String memberId,
									@RequestParam(required = true) Integer advertType) throws Exception {
		logBefore(logger, "获取广告列表");
		Map<String, Object> map = null;
		try {
			map = wxApiMemberService.getAdvertList(memberId,advertType);
			if(map == null || map.size()==0){
				throw new ApplicationException(Rescode.FAIL,"未查询到记录");
			}
			return Response.getSuccess(map);
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
	}

	/**
	 * 获取消息列表
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getMessageList", method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public Response getMessageList(@RequestParam(required = true) String memberId,
								  @RequestParam(required = true) Integer messageType,
								   @RequestParam(required = true) Integer pageNo,
								   @RequestParam(required = true) Integer pageSize) throws Exception {
		logBefore(logger, "获取消息列表");
		Map<String, Object> map = null;
		try {
			map = wxApiMemberService.getMessageList(memberId,messageType,pageNo,pageSize);
			if(map == null || map.size()==0){
				throw new ApplicationException(Rescode.FAIL,"未查询到记录");
			}
			return Response.getSuccess(map);
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
	}

	/**
	 * 更新消息
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/updateMessageById", method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public Response updateMessageById(@RequestParam(required = true) String memberId,
								   @RequestParam(required = true) Integer messageId,
								   @RequestParam(required = true) Integer status) throws Exception {
		logBefore(logger, "更新消息");
		Map<String, Object> map = null;
		try {
			wxApiMemberService.updateMessageById(memberId,messageId,status);
			return Response.getSuccess(map);
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
	}

	@RequestMapping("/rechargeBenefit")
	@ResponseBody
	public Response rechargeBenefit(@RequestParam Long memberId) {
		Response response=wxApiMemberService.rechargeBenefit(memberId);
		return response;
	}
	
	
	

	/**
	 * 服务商品抵用券
	 * @param memberId
	 * @param goodOrShopId
	 * @return
	 * @throws ApplicationException
	 */
	@RequestMapping(value = "/serviceCouponsList", method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public Response serviceCouponsList(Long memberId,Long goodOrShopId, Integer tradeMoney) throws ApplicationException {
		logBefore(logger, "服务商品抵用券");
		HashMap<String, String> resultMap = new HashMap<String, String>();
		resultMap.put("会员编号", memberId+"");
		resultMap.put("服务劵编号", goodOrShopId+"");
		resultMap.put("消费金额", tradeMoney+"");
		String nullParam=Response.checkNull(resultMap);
		if (null != nullParam) {
			return Response.getError(nullParam+"不能为空");
		}
		try {
			Response feeExplain=wxApiMemberService.getServiceCouponsList(memberId, goodOrShopId, tradeMoney);
			return feeExplain;
		} 
		catch (Exception e) {
			logger.error(e);
			return Response.getError("查询异常");
		}
		
	}


	/**
	 * 会员交易记录查询
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getMemberTradeList", method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public Response getMemberTradeList(@RequestParam(required = true) String memberId,
								@RequestParam(required = true) String datefrom,
								@RequestParam(required = true) String dateto,
								@RequestParam(required = false) String type,
								@RequestParam(required = false) String memo,
								@RequestParam(required = true) Integer pageNo,
								@RequestParam(required = true) Integer pageSize) throws Exception {
		logBefore(logger, "会员交易记录查询");
		Map<String, Object> map = null;

		try {
			map = tradeService.getMemberTradeList(memberId,datefrom,dateto,type,memo,pageNo,pageSize);
			if(map == null || map.size()==0){
				throw new ApplicationException(Rescode.FAIL,"未查询到记录");
			}
			return Response.getSuccess(map);
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
	}



	/**根据memberId查询会员余额
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/findMemberDisCount", method = {RequestMethod.GET,RequestMethod.POST} )
	@ResponseBody
	public Response findMemberDisCount(String memberId) throws Exception{

		logBefore(logger, "根据memberId查询会员折扣");
		if(Tools.isEmpty(memberId)){
			return Response.getError("memberId不能为空");
		}
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			map = wxApiMemberService.findMemberDisCount(memberId);
			if(map == null || map.size()==0){
				throw new ApplicationException(Rescode.FAIL,"未查询到记录");
			}
			return Response.getSuccess(map);
		} catch (ApplicationException e) {
			return Response.getError(e.errMsg);
		}catch (Exception e) {
			e.printStackTrace();
			return Response.getError("查询异常");
		}
	}


	@RequestMapping(value = "/couponVerify", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Response couponVerify(String couponCode) throws Exception {
		logBefore(logger, "门票核销");
		HashMap<String, String> resultMap = new HashMap<String, String>();
		resultMap.put("券码", couponCode);
		String nullParam = Response.checkNull(resultMap);
		if (null != nullParam) {
			return Response.getError(nullParam + "不能为空");
		}
		try {
			Response response = spotService.couponVerify(couponCode);
			return response;
		} catch (ApplicationException ae) {
			logger.error(ae);
			ae.printStackTrace();
			return Response.getError(ae.errMsg);
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			return Response.getError("服务器异常");
		}
	}


	/**
	 * 暫用
	 */

	@RequestMapping(value = "/addAgent", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Response addAgent(@RequestParam(required = true) String groupId,
							 @RequestParam(required = true) String principal,
							 @RequestParam(required = true) String principalMobile,
							 @RequestParam(required = true) String legalPersonId) throws Exception {
		logBefore(logger, "喜欢商户列表");

		HashMap<String, String> resultMap = new HashMap<String, String>();
		resultMap.put("商户编号", groupId);
		resultMap.put("用户名", principal);
		resultMap.put("用户手机号", principalMobile);
		resultMap.put("身份证号", legalPersonId);
		String nullParam = Response.checkNull(resultMap);
		if (null != nullParam) {
			return Response.getError(nullParam + "不能为空");
		}
		try {
			Response response = groupService.addAgent(groupId,principal,legalPersonId,principalMobile);
			return response;
		} catch (ApplicationException ae) {
			logger.error(ae);
			ae.printStackTrace();
			return Response.getError(ae.errMsg);
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			return Response.getError("服务器异常");
		}
	}

	/**
	 * 发送微信模板消息
	 * @param orderId	订单编号
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/sendWxTemplateMessage", method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public Response sendWxTemplateMessage(String orderId, String templatetype, String status) throws Exception {
		TradeOffline tradeOffline = tradeService.selectByOrderId(orderId);
		try {
			if(status == null) {
				templateMailSendService.sendTemplate(tradeOffline, templatetype, tradeOffline.getPayerId(), tradeOffline.getGroupId(), BizEnum.PaymentType.WebChat.getType(), ROOT_HOME_URL);
			} else {
				templateMailSendService.sendTemplate(tradeOffline, templatetype, tradeOffline.getPayerId(), tradeOffline.getGroupId(), BizEnum.PaymentType.WebChat.getType(), ROOT_HOME_URL, status);
			}
			return Response.buildSuccess("发送成功!");
		} catch (Exception e) {
			return Response.getError("服务器异常");
		}
	}


	/**
	 * 会员信息
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryMemberId", method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public Response queryMemberId( String shopId, String phone) throws Exception {
		logBefore(logger, "查询会员信息");
		HashMap<String, String> resultMap = new HashMap<String, String>();
		resultMap.put("门店编号", shopId);
		resultMap.put("手机号", phone);
		String nullParam=Response.checkNull(resultMap);
		if (null != nullParam) {
			return Response.getError(nullParam+"不能为空");
		}

		Map<String, Object> map = null;
		try {
			map = wxApiService.queryMemberId(shopId,phone);
			if(map == null || map.size()==0){
				throw new ApplicationException(Rescode.FAIL,"未查询到记录");
			}
			return Response.getSuccess(map);
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
	}


}
